
// <copyright file="DummyControllable.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The dummy controllable.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.TestApplication
{
    using System;
    using System.Threading;

    using SubhadraSolutions.Sharp.Utils.Contracts;

    /// <summary>
    /// The dummy controllable.
    /// </summary>
    public class DummyControllable : IControllable
    {
        #region Fields

        /// <summary>
        /// The controller.
        /// </summary>
        private readonly GenericMetricsEnabledComponentController _controller;

        /// <summary>
        /// The pause wait handle.
        /// </summary>
        private readonly ManualResetEvent _pauseWaitHandle = new ManualResetEvent(false);

        /// <summary>
        /// The random.
        /// </summary>
        private readonly Random _random = new Random();

        /// <summary>
        /// The should stop.
        /// </summary>
        private volatile bool _shouldStop;

        /// <summary>
        /// The thread.
        /// </summary>
        private Thread _thread;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DummyControllable"/> class.
        /// </summary>
        public DummyControllable()
        {
            this._controller = new GenericMetricsEnabledComponentController(this.start, this.stop, this.pause, this.resume);
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the controller.
        /// </summary>
        public IComponentController Controller
        {
            get
            {
                return this._controller;
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        #endregion Public Properties

        #region Methods

        /// <summary>
        /// The pause.
        /// </summary>
        private void pause()
        {
            this._pauseWaitHandle.Reset();
        }

        /// <summary>
        /// The resume.
        /// </summary>
        private void resume()
        {
            this._pauseWaitHandle.Set();
        }

        /// <summary>
        /// The start.
        /// </summary>
        private void start()
        {
            this._shouldStop = false;
            this._pauseWaitHandle.Set();
            this._thread = new Thread(this.threadStart) { IsBackground = true };
            this._thread.Start();
        }

        /// <summary>
        /// The stop.
        /// </summary>
        private void stop()
        {
            this._shouldStop = true;
            this._pauseWaitHandle.Set();
        }

        /// <summary>
        /// The thread start.
        /// </summary>
        private void threadStart()
        {
            while (true)
            {
                this._pauseWaitHandle.WaitOne();
                if (this._shouldStop)
                {
                    return;
                }

                this._controller.StartedDoingSomething();
                Thread.Sleep(this._random.Next(1000));
                this._controller.StoppedDoingSomething();
                Thread.Sleep(this._random.Next(1000));
            }
        }

        #endregion Methods
    }
}
